package by.exposit.service;

import by.exposit.dao.ProjectDAO;
import by.exposit.dao.RequestDAO;
import by.exposit.dao.UserDAO;
import by.exposit.dao.entity.FieldEntity;
import by.exposit.dao.entity.ProjectEntity;
import by.exposit.dao.entity.RequestEntity;
import by.exposit.dao.entity.UserEntity;
import by.exposit.domain.*;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HaRiA on 30.07.2014.
 */
@Service
@Transactional
public class RequestServiceImpl  implements RequestService {

    @Autowired
    private  RequestDAO requestDAO;
    @Autowired
   private ProjectDAO projectDAO;
    @Autowired
    private UserDAO userDAO;
    @Override
    public void saveRequest(Request request) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        requestDAO.save(mapper.map(request, RequestEntity.class));

    }

    @Override
    public List<Request> requestList() {
                Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
                List<Request> requests = new ArrayList<Request>();
        List<RequestEntity> requestEntities = requestDAO.getAll();
        for(RequestEntity requestEntity: requestEntities )
        {
               requests.add(mapper.map(requestEntity,Request.class));
        }
        return requests;
    }

    @Override
    public List<Request> requestUserList(Long userId) {

        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        List<Request> requests =   new ArrayList<Request>();
        UserEntity user  = userDAO.findByID(userId);
        List<ProjectEntity> projectEntities = projectDAO.projectUserList(user);
        List<RequestEntity> requestEntities =   null;

            for (ProjectEntity projectEntity : projectEntities) {
                RequestEntity requestEntity = requestDAO.getRequestByProject(projectEntity);
                if(requestEntity!=null) {
                    requests.add(mapper.map(requestDAO.getRequestByProject(projectEntity), Request.class));
                }
        }
       return requests;
    }

    @Override
    public List<Request> requestFieldList(User user) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        List<Request> requests =   new ArrayList<Request>();
        List<Field> fields  = new ArrayList<Field>();
        List<FieldEntity>  fieldsList;
        List<RequestEntity> tempRequestEntities= null;
         fieldsList = projectDAO.getFieldsInUsersProjects(mapper.map(user, UserEntity.class));
       /* for(FieldEntity fieldEntity:fieldsList)
        {
            fields.add(mapper.map(fieldEntity,Field.class));
        } */
         for(FieldEntity field:fieldsList) {
             tempRequestEntities = requestDAO.requestFieldList(field);

             for (RequestEntity requestEntity : tempRequestEntities) {
               if(requestEntity!=null){
                 requests.add(mapper.map(requestEntity, Request.class));
             }
             }

         }
        return requests;


    }

    @Override
    public void remove(Long requestId) {
        requestDAO.remove(requestId);
    }

    @Override
    public Request getRequestByProject(Project project) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        RequestEntity requestEntity =  requestDAO.getRequestByProject(mapper.map(project, ProjectEntity.class));
     Request request = null;
      if (requestEntity !=null) {
          request =  mapper.map(requestEntity, Request.class);
      }
        return request;
    }


    @Override
    public void changeStatusRequest(Request request, Status status) {
        requestDAO.changeStatusRequest(request,status);
    }

    @Override
    public void allocateMoney(Request  request) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        requestDAO.allocateMoney(mapper.map(request,RequestEntity.class));
    }

    @Override
    public List<Request> satisfyRequest() {
        return null;
    }

    @Override
    public List<Request> financedButNotExecuted() {
        return null;
    }

    @Override
    public List<Request> totalFundsAllocatedForField(Field field) {
        return null;
    }

    @Override
    public float percentageOfSuccessfullyCompletedProjects(Field field) {
        return requestDAO.percentageOfSuccessfullyCompletedProjects(field);
    }

    @Override
    public long sumForConsiderRequest(Field field) {
        return 0;
    }

    @Override
    public Request retrieveRequest(Long requestId) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        RequestEntity requestEntity = requestDAO.findByID(requestId);
        Request request =null;
        if(requestEntity!=null) {
             request =  mapper.map(requestEntity, Request.class);
        }

            return request;

    }
}
